//
//  TCM_AutionDetailsViewController.swift
//  TCM_TradingProject
//
//  Created by 吕春海 on 2018/8/29.
//  Copyright © 2018年 lvchunhai. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class TCM_AutionDetailsViewController: TCM_BaseViewController,UITableViewDelegate,UITableViewDataSource,CycleViewDelegate,rankViewDelegat {

    var autionTableView:UITableView!
    let disposeBag = DisposeBag()
    let detailFooterView = TCM_AuctionDetailFooterView()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "拍卖详情"
        navigationItem.rightBarButtonItem = UIBarButtonItem(title:"拍卖规则", style: .plain, target: self, action: #selector(rulesClick))
        
        initialTableView()
        // Do any additional setup after loading the view.
    }

    func initialTableView() {
        autionTableView = UITableView(frame: CGRect(x: 0, y: TCM_NavBarHeight(), width: TCM_ScreenWidth, height: TCM_ScreenHeight - TCM_NavBarHeight() - 44 - TCM_SafeBottomHeight()), style:.grouped)
        autionTableView.backgroundColor = TCM_HexRGB(0xF0F0F0)
        autionTableView.separatorStyle = UITableViewCellSeparatorStyle.none
        autionTableView.showsVerticalScrollIndicator = false
        autionTableView.delegate = self
        autionTableView.dataSource = self
        
        self.view.addSubview(autionTableView)
        
        detailFooterView.frame = CGRect(x: 0, y: 0, width: TCM_ScreenWidth, height: 246)
        autionTableView.tableFooterView = detailFooterView
        detailFooterView.tableView = autionTableView
        
        let cycleView : CycleView = CycleView(frame: CGRect(x: 0, y: TCM_NavBarHeight(), width: TCM_ScreenWidth, height: 212))
        cycleView.delegate = self
        cycleView.mode = .scaleAspectFill
        //本地图片测试--加载网络图片,请用第三方库如SDWebImage等
        cycleView.imageURLStringArr = ["banner01.jpg", "banner02.jpg", "banner03.jpg", "banner04.jpg", "banner05.jpg"]
        autionTableView.tableHeaderView = cycleView
        
        autionTableView.register(UINib.init(nibName: "TCM_MedicineTableViewCell", bundle: nil), forCellReuseIdentifier: "medicineCell")
        autionTableView.register(UINib.init(nibName: "TCM_AuctionDetailsTableViewCell", bundle: nil), forCellReuseIdentifier: "auctionDetailsCell")
        
    }
    
    // MARK: -tableview代理
    //设置cell的数量
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    //设置section的数量
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 72
        } else {
            return 208
        }
    }
    
    //设置tableview的cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = (autionTableView.dequeueReusableCell(withIdentifier: "medicineCell", for: indexPath)) as! TCM_MedicineTableViewCell
            cell.selectionStyle = .none
            return cell
        } else {
            let cell = (autionTableView.dequeueReusableCell(withIdentifier: "auctionDetailsCell", for: indexPath)) as! TCM_AuctionDetailsTableViewCell
            cell.selectionStyle = .none
            cell.delegate = self
            return cell
        }
        
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.00001
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 8
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerView = UIView()
        footerView.frame = CGRect(x: 0, y: 0, width: TCM_ScreenWidth, height: 8)
        footerView.backgroundColor = TCM_HexRGB(0xF0F0F0)
        return footerView
    }
    
    // MARK: --cycleviewDelegate
    func cycleViewDidSelectedItemAtIndex(_ index: NSInteger) {
        print("点击了轮播图第\(index + 1)个图片")
        //        let demoVc = DemoController()
        //        demoVc.title = "点击了轮播图第\(index)个图片"
        //        demoVc.view.backgroundColor = UIColor.white
        //        navigationController?.pushViewController(demoVc, animated: true)
    }
    
    // MARK: --rankViewDelegat
    func rankButtonClick() {
        let rankVC = TCM_AuctionRankViewController()
        self.navigationController?.pushViewController(rankVC, animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func payMarginClick(_ sender: UIButton) {
        let detailsVC = TCM_PayMarginViewController()
        self.navigationController?.pushViewController(detailsVC, animated: true)
    }
    
    @objc func rulesClick() {
        let detailsVC = TCM_AuctionRulesViewController()
        self.navigationController?.pushViewController(detailsVC, animated: true)
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
